/**
 * Created by Even on 2018/1/18.
 */
import router from '@/router'

// 跳转方法
export const nextPage = (url, param) => {
  let obj = { path: url, query: param }
  router.push(obj)
}
// 将14位日期时间转换为标准时间
export const dateFormat = (val) => {
  // 不兼容火狐和IE
  // let arr = val.replace(/^(\d{8})(\d{6})$/, '$1').replace(/^(\d{4})(\d{2})(\d{2})$/, '$1.$2.$3')
  // return new Date(arr)
  let arr = val.replace(/^(\d{8})(\d{6})$/, '$1').replace(/^(\d{4})(\d{2})(\d{2})$/, '$1,$2,$3').split(',')
  return new Date(new Date().setFullYear(Number(arr[0]), Number(arr[1]) - 1, Number(arr[2])))
}

// 将中国标准时间转换为14位日期格式
/**
 * @param {*} ms 中国标准时间
 * @param {*} val 判断返回格式
 */
export const getDateTime = (ms, val) => {
  let Y = ms.getFullYear()
  let M = ms.getMonth() + 1
  if (M < 10) {
    M = '0' + M
  }
  let D = ms.getDate()
  if (D < 10) {
    D = '0' + D
  }
  let h = ms.getHours()
  if (h < 10) {
    h = '0' + h
  }
  let m = ms.getMinutes()
  if (m < 10) {
    m = '0' + m
  }
  let s = ms.getSeconds()
  if (s < 10) {
    s = '0' + s
  }
  if (val === 's') {
    return '' + Y + M + D + h + m + s
  } else if (val === 'ss') {
    return Y + '-' + M + '-' + D + ' ' + h + ':' + m + ':' + s
  } else if (val === 'day') {
    return Y + '-' + M + '-' + D
  } else {
    return Y + '-' + M + '-' + D + ' ' + h + ':' + m
  }
}

// 将中国标准时间转换为8位数日期格式
/**
 * @param {*} ms 中国标准时间
 *
 */
export const getDateTime1 = (ms) => {
  let Y = ms.getFullYear()
  let M = ms.getMonth() + 1
  if (M < 10) {
    M = '0' + M
  }
  let D = ms.getDate()
  if (D < 10) {
    D = '0' + D
  }
  return '' + Y + M + D
}

// 时间格式化，时间自增
export const getFormatTime = (ms, vm) => {
  let Y = ms.getFullYear()
  let M = ms.getMonth() + 1
  let D = ms.getDate()
  let h = ms.getHours()
  let m = ms.getMinutes()
  let s = ms.getSeconds()
  if (M <= 9) {
    M = `0${M}`
  }
  if (D <= 9) {
    D = `0${D}`
  }
  setInterval(() => {
    s++
    if (s < 10) {
      s = `0${s}`
    } else if (s > 59) {
      s = 0 + '0'
      m++
    }
    if (m <= 9 && (m + '').length < 2) {
      m = `0${m}`
    } else if (m > 59) {
      m = 0
      h++
    }
    if (h <= 9 && (h + '').length < 2) {
      h = `0${h}`
    } else if (h > 24) {
      h = 1
    }
    vm.time = Y + '-' + M + '-' + D + ' ' + h + ':' + m + ':' + s
  }, 1000)
  return vm.time
}

// 数字千位符格式化
export const toThousands = (num) => {
  let result = [ ]
  let counter = 0
  num = (num || 0).toString().split('')
  for (let i = num.length - 1; i >= 0; i--) {
    counter++
    result.unshift(num[i])
    if (!(counter % 3) && i !== 0) {
      result.unshift(',')
    }
  }
  return result.join('')
}

// 日期格式化
export const formatTransDate = (date) => {
  const data = date.replace(/^(\d{4})(\d{2})(\d{2})$/, '$1.$2.$3')
  return data
}
// 自定义日期格式
/* export const formatTimeToDate = (time, oprType = '.') => {
  let date = new Date(time)

  const data = date.replace(/^(\d{4})(\d{2})(\d{2})$/, '$1' + oprType + '$2' + oprType + '$3')
  return data
} */
// 后台树到前台vue树的数据结构转换
export const transTreeDataToVue = (treeNodes) => {
    let layerTreeDatas = []
    for (let treeNode of treeNodes) {
      if (!treeNode.obj) {
        throw Error('数据格式不正确!')
      }
      let layerTreeData = JSON.parse(JSON.stringify(treeNode.obj))
      delete layerTreeData.children
      layerTreeDatas.push(layerTreeData)

      let children = treeNode.children
      if (children && children.length) {
        let childLayerTreeDatas = transTreeDataToVue(children)
        layerTreeData['children'] = childLayerTreeDatas
      }
    }
    return layerTreeDatas
}

/**
 * 随机生成16进制的颜色代码
 * @returns {string}
 */
export function randomColor () {
    let colorArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'a', 'b', 'c', 'd', 'e', 'f']
    let color = '#'
    for (let i = 0; i < 6; i++) {
        color = color + colorArray[Math.floor(Math.random() * 16)]
    }
    return color
}

/**
 * 原生js 滚动方法
 * @description 原生js 滚动，实现 任意元素 滚动
 * @author kayle
 * @param {HTMLElement} node - dom节点
 * @param {Number} number - scrollTop值，改节点已向上滚动过的值
 * @param {Number} time - 时间 单位ms，滚动动画的持续时间
 * @return {Number} - number，标识 滚动结束
 */
export const scrollTop = (node = document.body, number = 0, time) => {
  if (!time) {
    node.scrollTop = number
    return number
  }
  const spacingTime = 20 // 设置循环的间隔时间  值越小消耗性能越高
  let spacingInex = time / spacingTime // 计算循环的次数
  let nowTop = node.scrollTop // 获取当前滚动条位置
  let everTop = (number - nowTop) / spacingInex // 计算每次滑动的距离
  let scrollTimer = setInterval(() => {
    if (spacingInex > 0) {
      spacingInex--
      scrollTop(node, nowTop += everTop)
    } else {
      clearInterval(scrollTimer) // 清除计时器
    }
  }, spacingTime)
}

// 毫秒数获取当前时间，格式YYYY-MM-DD
export const getNowFormatDate = (val) => {
  let date = new Date(val)
  let seperator1 = '-'
  let year = date.getFullYear()
  let month = date.getMonth() + 1
  let strDate = date.getDate()
  if (month >= 1 && month <= 9) {
      month = '0' + month
  }
  if (strDate >= 0 && strDate <= 9) {
      strDate = '0' + strDate
  }
  return year + seperator1 + month + seperator1 + strDate
}
